import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:xindong/common/xindong_app_info.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/common/xindong_methods.dart';
import 'package:xindong/common/xindong_storage.dart';
import 'package:xindong/manager/xindong_audio_ctr.dart';
import 'package:xindong/manager/xindong_database_singleton.dart';
import 'package:xindong/manager/xindong_shengyin_zhendong_singleton.dart';
import 'package:xindong/models/xindong_banner_class.dart';
import 'package:xindong/models/xindong_city_class.dart';
import 'package:xindong/models/xindong_name_class.dart';
import 'package:xindong/models/xindong_tag_class.dart';
import 'package:xindong/models/xindong_word_class.dart';
import 'package:xindong/plugins/xindong_from_native_plugin.dart';
import 'package:xindong/plugins/xindong_to_native_plugin.dart';
import 'package:xindong/res/assets_res.dart';
import 'package:xindong/utils/xindong_image_util.dart';
import 'package:xindong/widgets/video/xindong_common_video_list_ctr.dart';

class XinDongAll {
  bool isRelease = kReleaseMode;
  bool smartDialogInitialized = false;

  // 应用是在前台活跃状态
  bool applicationActive = true;

  final Widget defaultAvatar = ImageUtil.asset(AssetsRes.DEFAULT_AVATAR);
  final Widget defaultIconNext = const Icon(Icons.navigate_next, color: DZColors.placeholderColor);
  final Widget defaultImage = Container(color: DZColors.coverColor);

  // 配置
  Future<void> setup() async {
    await XinDongStorage.instance.setup(); // 这个是异步的，避免读取有误
    await XinDongAppDeviceInfo.instance.getAppInfo();
    await XinDongDatabaseSingleton.instance.setup();
    await XinDongToNativePlugin.instance.androidChannelData(); // 安卓要获取包的渠道
    XinDongShengYinZhenDongSingleton.instance;
    XinDongFromNativePlugin().setupListen();
    DZColors.init();
    // 智能弹窗
    SmartDialog.config
      ..custom = SmartConfigCustom(maskColor: DZColors.maskColor)
      ..attach = SmartConfigAttach(maskColor: DZColors.maskColor)
      ..loading = SmartConfigLoading(maskColor: DZColors.maskColor)
      ..toast = SmartConfigToast(maskColor: DZColors.maskColor);

    // 本地保存的邀请信息
    String? inviteCode = XinDongStorage.instance.getString(XinDongStorageKey.invite);
    if (inviteCode != null) {
      XinDongAll.instance.inviteCode = inviteCode;
    }

    return Future.value();
  }

  // 暂停所有播放器播放
  void pauseAllPlayer() {
    // 暂停动态视频的播放
    try {
      XinDongAudioCtr.to.clear();
      XinDongCommonVideoListCtr.to.clear();
    } catch (error) {
      mCatchPrint(error);
    }
  }

  String office = "flutter";
  late String androidChannel = office;

  // 邀请码，为null时代表没有获取到超链数据
  String? inviteCode;

  // 职业
  List<XinDongNameClass> occupationList = [];

  // 标签
  List<XinDongTagClass> tagList = [];

  // 聊天用语
  List<XinDongWordClass> chatCommonWords = [];

  // 轮播图
  List<XinDongBannerClass> slideList = [];

  // ======== 单例
  // 工厂方法构造函数
  factory XinDongAll() => _getInstance();

  // instance的getter方法，singletonManager.instance获取对象
  static XinDongAll get instance => _getInstance();

  // 静态变量_instance，存储唯一对象
  static XinDongAll? _instance;

  // 获取对象
  static XinDongAll _getInstance() {
    _instance ??= XinDongAll._internal();
    return _instance!;
  }

  // 私有的自定义命名式构造方法，通过它实现一个类;
  // 子类不能继承internal
  // 不是关键字，可定义其他名字
  XinDongAll._internal() {
    //初始化(设置一些默认的)...
  }
}
